package com.vf.admin.miaodong.dao;

import com.vf.admin.client.dto.AppNewDto;
import com.vf.admin.common.db.BaseDao;
import com.vf.admin.common.splitpage.SplitPageBean;
import com.vf.admin.miaodong.entity.AppCommentReply;
import com.vf.admin.miaodong.entity.AppNew;
import com.vf.admin.miaodong.entity.AppUser;
import com.vf.admin.utils.StringEx;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 会话
 */
@Repository
public class AfterNewDao extends BaseDao<AppNew> {

    /**
     * 查询列表
     */
    public List<AppNew> list(Map<String,Object> paraMap, SplitPageBean splitPage) {
        ArrayList<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_news where 1=1  ");
        sbStr.append(" order by createtime desc");
        String sql = sbStr.toString();
        return this.queryList(sql,paramList, AppNew.class,splitPage);
    }


    /**
     * 详情
     */
    public AppNew get(Integer id) {
        if (StringEx.isNullOrLing(id)) {
            return null;
        }
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("select * from app_news where 1=1");
        sbStr.append(" and id = ? ");
        paramList.add(id);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppNew.class);
    }

    /**
     * 添加/修改
     */
    public void save(AppNew haoYou) {
        if (haoYou.getId() != null && haoYou.getId() > 0) {
            this.update(haoYou);
        } else {
            haoYou.setId(this.add(haoYou));
        }
    }

    /**
     * 删除
     */
    public void del(Integer id) {
        if (id <= 0) {
            return;
        }
        String sql = "delete  from app_news where id = ?";
        ArrayList<Object> paramList = new ArrayList<Object>();
        paramList.add(id);
        this.del(sql, paramList);
    }

    /**
     * 综合查询
     * @param map
     * @param bean
     * @return
     */
    public List<AppNew> findList(Map<String, Object> map, SplitPageBean bean) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append(" SELECT a.* FROM app_news a where 1=1 ");
        if (StringEx.isNotNull(map.get("nick"))) {
            sbStr.append(" and a.nick like ? ");
            paramList.add("%" + map.get("nick") + "%");
        }
        if (StringEx.isNotNull(map.get("phone"))) {
            sbStr.append(" and a.phone = ? ");
            paramList.add(map.get("phone"));
        }
        if (!StringEx.isNull(map.get("startDate"))) {
            sbStr.append(" and a.createtime>=? ");
            paramList.add(map.get("startDate"));
        }
        if (!StringEx.isNull(map.get("endDate"))) {
            sbStr.append(" and a.createtime<=? ");
            paramList.add(map.get("endDate"));
        }
        sbStr.append(" order by a.createtime desc ");
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppNew.class, bean);
    }


    /**
     * 根据用户ID和对方用户id查询会话记录
     * @param userid
     * @param dfuseridid
     * @return
     */
    public AppNew getHuiHa(Integer userid, Integer dfuseridid) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select * from app_news  where userid = ? and dfuseridid = ?");
        paramList.add(userid);
        paramList.add(dfuseridid);
        String sql = sbStr.toString();
        return this.queryObj(sql, paramList, AppNew.class);
    }

    /**
     * 根据用户ID查询会话记录列表
     * @param userid
     * @param page
     * @return
     */
    public List<AppNewDto> getConversation(Integer userid, SplitPageBean page) {
        List<Object> paramList = new ArrayList<Object>();
        StringBuilder sbStr = new StringBuilder("");
        sbStr.append("select * from app_news  where userid = ?");
        paramList.add(userid);
        String sql = sbStr.toString();
        return this.queryList(sql, paramList, AppNewDto.class, page);
    }


}
